#!/usr/bin/env bash

quick_report() {
    local GPUS
    local OS_KERNEL
    local PRETTY_NAME
    local QUICKEMU
    local VERSION
    OS_KERNEL=$(uname -s)

    if [ "${OS_KERNEL}" == "Darwin" ]; then
        # Get macOS product name and version using swvers
        if [ -x "$(command -v sw_vers)" ]; then
            PRETTY_NAME="$(sw_vers -productName) $(sw_vers -productVersion)"
        else
            PRETTY_NAME="macOS"
        fi
    elif [ -e /etc/os-release ]; then
        PRETTY_NAME=$(grep PRETTY_NAME /etc/os-release | cut -d'"' -f2)
    else
        PRETTY_NAME="Unknown OS"
    fi

    CWD="$(dirname "${0}")"
    if [ -x "${CWD}/quickemu" ]; then
        QUICKEMU="${CWD}/quickemu"
    elif [ -x "$(command -v quickemu)" ]; then
        QUICKEMU="$(command -v quickemu)"
    fi

    if [ -n "${QUICKEMU}" ]; then
        VERSION=$(${QUICKEMU} --version)
        echo \
"----------------------------------
        Quickemu ${VERSION}
----------------------------------"
        echo -e "Distro:\t${PRETTY_NAME}"
        echo -e "Kernel:\t$(uname -s -r -m)"

        if [ "${OS_KERNEL}" == "Darwin" ]; then
            echo -e "Memory:\t$(($(sysctl -n hw.memsize) / (1048576*1024)))G"
        else
            # Determine the number of gigabytes of RAM in the host by extracting the first numerical value from the output.
            echo -e "Memory:\t$(free --giga -h | tr ' ' '\n' | grep -m 1 "[0-9]" | cut -d'G' -f 1)G"
        fi

        # Break IFS on new line
        IFS=$'\n'
        if [ "${OS_KERNEL}" == "Darwin" ]; then
            # Get GPU information using system_profiler
            GPUS=$(system_profiler SPDisplaysDataType | grep "Chipset Model" | awk -F: '{print $2}' | sed 's/^ *//')
        else
            GPUS=$(lspci | grep -i vga | cut -d':' -f3)
        fi

        if [ "$(echo "${GPUS}" | wc -l)" -eq 1 ]; then
            echo "GPU:"
        else
            echo "GPUs:"
        fi
        for GPU in ${GPUS}; do
            echo " -${GPU}"
        done
    else
        echo \
"----------------------------------
        Quickemu missing!
----------------------------------"
        exit 1
    fi

    if command -v curl &> /dev/null; then
        VERSION=$(curl --version)
        echo \
"----------------------------------
            curl $(echo "${VERSION}" | head -n 1 | cut -d' ' -f2)
----------------------------------"
        echo -e "Libraries:$(echo "${VERSION}" | head -n 1 | cut -d')' -f2-)"
        echo -e "Protocols:$(echo "${VERSION}" | tail -n +3 | head -n 1 | cut -d':' -f2-)"
        echo -e "Features: $(echo "${VERSION}" | tail -n +4 | head -n 1 | cut -d':' -f2-)"
    else
        echo \
"----------------------------------
            curl missing
----------------------------------"
    fi

    local HOST_ARCH
    HOST_ARCH=$(uname -m)
    local QEMU_ARCH="${HOST_ARCH}"
    if [ "${HOST_ARCH}" == "arm64" ]; then
        QEMU_ARCH="aarch64"
    fi

    if command -v "qemu-system-${QEMU_ARCH}" &> /dev/null; then
        VERSION=$("qemu-system-${QEMU_ARCH}" --version | head -n 1 | cut -d' ' -f4)
        echo \
"----------------------------------
            QEMU ${VERSION}
----------------------------------"
        "qemu-system-${QEMU_ARCH}" -cpu help
    else
        echo \
"----------------------------------
            QEMU missing
----------------------------------"
    fi

    echo \
"----------------------------------
               CPU
----------------------------------"
    if [ "${OS_KERNEL}" == "Darwin" ]; then
        sysctl -n machdep.cpu.brand_string
    else
        lscpu
    fi
}

clear
quick_report | tee quickreport.txt
